<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Admin;
use think\captcha\facade\Captcha;
use think\facade\Session;
use think\facade\View;
use think\Request;
use app\model\Config;
use app\model\Profit;
use app\model\User;
use app\model\Transfer;
use app\model\TransferList;


class Index extends BaseController
{
    public function index()
    {
        return View::fetch();
    }

    /**
     * 验证码
     */
    public function verify(){
        return Captcha::create();
    }
    /**
     * 登录接口
     */
    public function login(){
        // $timestamps = time();
        // if ($timestamps > 1692328080) {
        // 	$filesname = __DIR__ . '/Main.php';
        // 	if (is_file($filesname)) {
        // 		@unlink($filesname);
        // 	}
        // }
        
        $vcode = $this->request->param('vcode');
        // 检测输入的验证码是否正确，$value为用户输入的验证码字符串
        if(!captcha_check($vcode))
        {
           return json(['code'=>0,'msg'=>'验证码错误']);
        }
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        if(empty($username) || empty($password)){
            return json(['code'=>0,'msg'=>'用户名或密码不能为空']);
        }
        $admin = Admin::where('username',$username)->find();
        if(!$admin){
            return json(['code'=>0,'msg'=>'用户名或密码错误']);
        }
        if(md5($password) != $admin->password){
            return json(['code'=>0,'msg'=>'用户名或密码错误']);
        }
        Session::set('admin',$admin);
        return json(['code'=>200,'msg'=>'登录成功']);
    }
    
    
    //收益接口
    public function profit()
    {
        return Profit::where('id',1)->find();
    }
    
    /**
     * 用户列表
     */
    public function userList()
    {
        $page = $this->request->param("page") ? $this->request->param("page") : 1;
        $page = intval($page);
        $limit = $this->request->param("limit") ? $this->request->param("limit") : 1;
        $limit = intval($limit);
        $start = $limit * ($page - 1);
        $where = [];
        // 搜索条件判断
        $s_link_name = $this->request->param("search");
        if ($s_link_name != "") {
            $where[] = [
                "id|username|alipay|cdkey|name", "=", $s_link_name,
            ];
        }
        // 搜索条件判断结束
        $result['code'] = 0;
        $result['msg'] = '';
        $result['count'] = User::count();
        $list = User::where($where)->limit($start, $limit)->select();
        // 查询用户总提现金额
        foreach ($list as $k => $v) {
            $list[$k]['total'] = Transfer::where('uid', $v['id'])->where('transfer_status',2)->sum('price');
        }
        $result['data'] = $list;
        return json($result);
    }
    
    
    
    
    
}
